//
//  BrokenLineView.m
//  BrokenLine
//
//  Created by 彤辉 沈 on 12-2-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BrokenLineView.h"

@implementation BrokenLineView

@synthesize groupData;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
	CGContextFillRect(context, rect);
    
    //定义统计图在rect中的位置
    marginTop = 20;
    marginBottom = 30;
    marginLeft = 30;
    marginRight = 40;
    
    //默认刻度数为5
    columnCount = 5;
	
	//计算各种数值
	[self calculateScale:rect];
    
    //画折线
	[self drawColumn:context rect:rect];
	
	//画刻度
	[self drawScale:context rect:rect];
}

-(void)calculateScale:(CGRect)rect{
    //找到横坐标的最大值和最小值，并计数
    NSNumber* num = [[groupData objectAtIndex:0]objectAtIndex:0];
    minSide = [num intValue];
    for(NSNumber* number in [groupData objectAtIndex:0]){
        if (maxSide<[number intValue]) maxSide = [number intValue];
        if (minSide>[number intValue]) minSide = [number intValue];
        sideCount++;
    }
    
    //计算sideWidth
    sideWidth= (rect.size.width - marginLeft - marginRight-20)/(maxSide-minSide);
    
    //找到竖坐标的最大值和最小值
    for(NSNumber* number in [groupData objectAtIndex:1]){
        if (maxValue<[number intValue]) maxValue = [number intValue];
        if (minValue>[number intValue]) minValue = [number intValue];
    }
    //调整maxValue的值为columnNumber的整倍数
    maxValue = maxValue - maxValue%columnCount + columnCount;
    //计算columnHeight
    columnHeight = (rect.size.height - marginTop - marginBottom)/columnCount;
    //计算heightPerMark
    heightPerMark = columnHeight/(maxValue/columnCount);
}

-(void)drawScale:(CGContextRef)context rect:(CGRect)rect{
    //画垂线与水平线
    CGPoint points[3];
    points[0] = CGPointMake(marginLeft, marginTop-10);
    points[1] = CGPointMake(marginLeft, rect.size.height-marginBottom);
    points[2] = CGPointMake(rect.size.width-marginRight, rect.size.height-marginBottom);
    CGContextSetAllowsAntialiasing(context, NO);
    CGContextAddLines(context, points, 3);
    
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); //??不知道是什么意思
    for (int i=0; i<=columnCount; i++) {
        //将数字写在图上
        int Scale = (maxValue/columnCount)*i;
        NSString* scaleString = [NSString stringWithFormat:@"%i",Scale];
        [scaleString drawAtPoint:CGPointMake(marginLeft-10-[scaleString sizeWithFont:[UIFont systemFontOfSize:12]].width, rect.size.height-marginBottom-columnHeight*i-8) withFont:[UIFont systemFontOfSize:12]];
        //画出小刻度
        points[0] = CGPointMake(marginLeft, rect.size.height-marginBottom-columnHeight*i);
        points[1] = CGPointMake(marginLeft-5, rect.size.height-marginBottom-columnHeight*i);
        CGContextSetAllowsAntialiasing(context, NO);
        CGContextAddLines(context, points, 2);
        
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
		CGContextDrawPath(context, kCGPathStroke);
        
        //画水平虚线
        points[1] = CGPointMake(rect.size.width-marginRight, rect.size.height-marginBottom-columnHeight*i);
        CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1].CGColor);//设置颜色
        float partren[] = {2,3};
		CGContextSetLineDash(context, 0,partren , 2);//设置虚线
        CGContextAddLines(context, points, 2);
        
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
		CGContextDrawPath(context, kCGPathStroke);
    }
    
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);//??不知道是什么意思
	CGContextDrawPath(context, kCGPathStroke);//??不知道是什么意思
}

-(void)drawColumn:(CGContextRef)context rect:(CGRect)rect{
    //计算各个转折点的坐标
    CGPoint points[sideCount];
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    for (int i=0; i<sideCount; i++) {
        points[i] = CGPointMake(marginLeft+sideWidth*([(NSNumber*)[[groupData objectAtIndex:0]objectAtIndex:i]intValue]-minSide)+10, rect.size.height-marginBottom-heightPerMark*[(NSNumber*)[[groupData objectAtIndex:1]objectAtIndex:i]intValue]);
        //画在转折点上的黑点
        CGContextFillEllipseInRect(context, CGRectMake(points[i].x-5, points[i].y-5, 10, 10));
    }
    CGContextSetAllowsAntialiasing(context, NO);
    CGContextAddLines(context, points, sideCount);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextDrawPath(context, kCGPathStroke);
    //画下标
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);//为什么没有这句话，就无法显示下标？
    NSString* x;
    for (int i=0; i<sideCount; i++) {
        //得到下标值
        x = [NSString stringWithFormat:@"%i",[(NSNumber*)[[groupData objectAtIndex:0]objectAtIndex:i]intValue]];
        [x drawAtPoint:CGPointMake(marginLeft+sideWidth*([(NSNumber*)[[groupData objectAtIndex:0]objectAtIndex:i]intValue]-minSide)+7, rect.size.height-marginBottom) withFont:[UIFont systemFontOfSize:12]];
    }
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextDrawPath(context, kCGPathStroke);
}

@end
